package wng.su;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;


// 将Activity的名字压缩到一个字符，最终apk中可以节省十几个字节
public class M extends Activity implements CompoundButton.OnCheckedChangeListener {

    LinearLayout root;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        root = new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);
        setContentView(root);
        addButton("单行显示",0x1);
        addButton("统计下载",0x2);
        addButton("统计上传",0x4);
        addButton("自动隐藏",0x8);
        addButton("显示秒钟",0x10);
        TextView tv = new TextView(this);
        tv.setText("开通知权限（自己去设），加入白名单，设置自启，没了");
        root.addView(tv);
        startService(new Intent(this, S.class));
    }
    // 封装成方法调用可以继续节省字节。（如果将方法名字压缩到一个字符还可以继续减少）
    void addButton(String title,int flag){
        SharedPreferences p = getSharedPreferences("0",0);
        int g = p.getInt("f",-1);
        int n = (int) (getResources().getDisplayMetrics().density * 20f);
        CheckBox cb = new CheckBox(this);
        cb.setChecked((g & flag) > 0);
        cb.setText(title);
        cb.setTag(flag);
        cb.setOnCheckedChangeListener(this);
        cb.setPadding(n,n,n,n);
        root.addView(cb);
    }
    // 通过位运算保存在同一个地方能减少代码量也可以减小最终生成的文件大小
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        int f = (int) compoundButton.getTag();
        SharedPreferences p = getSharedPreferences("0",0);
        int g = p.getInt("f",-1);
        if(b){
            g = g | f;
        }
        else{
            g = g & (~f);
        }
        p.edit().putInt("f",g).apply();
        Intent i = new Intent(this, S.class);
        stopService(i);
        startService(i);
    }
}